/**
 * 查看坐骑基础数据统计信息
 */

const mysql = require('mysql2/promise');

const dbConf = {
  host: '43.143.253.188',
  port: 3306,
  user: 'nextjs',
  password: 'yEAiRE2jcxTyRBy5',
  database: 'nextjs',
  charset: 'utf8mb4'
};

// 坐骑类型映射
const MOUNT_TYPE_MAP = {
  1: '战马',
  2: '名马',
  3: '神马'
};

async function showMountStats () {
  const conn = await mysql.createConnection(dbConf);
  try {
    console.log('📊 坐骑基础数据统计信息:');

    // 总体统计
    const [total] = await conn.execute('SELECT COUNT(*) as total FROM mount_base');
    console.log(`\n总计: ${total[0].total} 条坐骑记录`);

    // 按类型统计
    const [typeStats] = await conn.execute(`
            SELECT 
                type,
                COUNT(*) as count,
                GROUP_CONCAT(name ORDER BY id SEPARATOR ', ') as names
            FROM mount_base 
            GROUP BY type
            ORDER BY type
        `);

    console.log('\n按坐骑类型统计:');
    typeStats.forEach(stat => {
      const typeName = MOUNT_TYPE_MAP[stat.type] || '未知';
      console.log(`  ${typeName} (类型${stat.type}): ${stat.count} 匹`);
      console.log(`    坐骑: ${stat.names}`);
    });

    // 属性统计
    const [attrStats] = await conn.execute(`
            SELECT 
                name,
                type,
                ap, apIni,
                hp, hpIni,
                mp, mpIni,
                sp, spIni
            FROM mount_base 
            ORDER BY type, id
        `);

    console.log('\n📋 坐骑详细属性:');
    console.table(attrStats.map(row => ({
      '坐骑名称': row.name,
      '类型': MOUNT_TYPE_MAP[row.type] || '未知',
      '攻击力': row.ap === -1000 ? '无' : row.ap,
      '初始攻击': row.apIni === -1000 ? '无' : row.apIni,
      '生命值': row.hp === -1000 ? '无' : row.hp,
      '初始生命': row.hpIni === -1000 ? '无' : row.hpIni,
      '法力值': row.mp === -1000 ? '无' : row.mp,
      '初始法力': row.mpIni === -1000 ? '无' : row.mpIni,
      '速度': row.sp === -1000 ? '无' : row.sp,
      '初始速度': row.spIni === -1000 ? '无' : row.spIni
    })));

    // 属性分布统计
    const [distStats] = await conn.execute(`
            SELECT 
                SUM(CASE WHEN ap != -1000 THEN 1 ELSE 0 END) as ap_count,
                SUM(CASE WHEN hp != -1000 THEN 1 ELSE 0 END) as hp_count,
                SUM(CASE WHEN mp != -1000 THEN 1 ELSE 0 END) as mp_count,
                SUM(CASE WHEN sp != -1000 THEN 1 ELSE 0 END) as sp_count,
                COUNT(*) as total
            FROM mount_base
        `);

    const dist = distStats[0];
    console.log('\n📈 属性分布统计:');
    console.log(`  攻击力坐骑: ${dist.ap_count}/${dist.total} (${(dist.ap_count / dist.total * 100).toFixed(1)}%)`);
    console.log(`  生命值坐骑: ${dist.hp_count}/${dist.total} (${(dist.hp_count / dist.total * 100).toFixed(1)}%)`);
    console.log(`  法力值坐骑: ${dist.mp_count}/${dist.total} (${(dist.mp_count / dist.total * 100).toFixed(1)}%)`);
    console.log(`  速度坐骑: ${dist.sp_count}/${dist.total} (${(dist.sp_count / dist.total * 100).toFixed(1)}%)`);

  } finally {
    await conn.end();
  }
}

if (require.main === module) {
  showMountStats().catch((err) => {
    console.error('查询失败:', err.message);
    process.exit(1);
  });
}

module.exports = { showMountStats };

